// 引入公共css
require("../../assets/css/normalize.css");
require("../../assets/css/reset.css");
require("../../assets/css/basic.css");
require("../../assets/css/property.css");
require("../../assets/fonts/word-img/iconfont.css");
let dom = require("../../assets/js/util.js");
// 引入自己的
require("./register.css");
// 引入axiosjs
const axios = require("axios");
// 引入随机验证码
const CaptchaMini = require("captcha-mini");

// 页面加载后
window.addEventListener("DOMContentLoaded", function () {
	// 验证码
	let codeinit = "";
	// 实例化
	let captcha = new CaptchaMini({
		fontSize: 65, //字体大小
	});
	// 调用
	captcha.draw(dom.$("#captcha"), (r) => {
		codeinit = r.toLowerCase();
	});

	// 验证
	let btn = dom.$("button");
	//绑定事件
	btn.addEventListener("click", function () {
		//获取所有的节点
		let account = dom.$("#tel");
		let code = dom.$("#code");
		let password1 = dom.$("#password1");
		let password2 = dom.$("#password2");

		//验证手机

		let reg1 = /^1[3-9]\d{9}$/;
		if (reg1.test(account.value)) {
			account.style.border = "1px solid #0a0";
		} else {
			account.style.border = "1px solid #f00";
			return;
		}

		// 验证码

		if (code.value.toLowerCase() === codeinit) {
			code.style.border = "1px solid #0a0";
		} else {
			code.style.border = "1px solid #f00";
			return;
		}

		// 验证密码;

		let reg2 = /^[\w]{6,}$/i;
		if (reg2.test(password1.value)) {
			password1.style.border = "1px solid #0a0";
		} else {
			password1.style.border = "1px solid #f00";
			return;
		}

		// 再次验证密码

		if (password2.value === password1.value) {
			password2.style.border = "1px solid #0a0";
		} else {
			password2.style.border = "1px solid #f00";
			return;
		}

		//以上都通过发请求
		axios
			.post("http://fitness.h5.itsource.cn:3701/api/user/register", {
				account: account.value,
				password1: password1.value,
			})
			.then((res) => {
				if (res.data.errno === 0) {
					location.href = "login.html";
				} else {
					alert("登陆失败" + res.data.message);
				}
			});
	});
});
